package com.sohu.peak.util;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;



public class ToolUtil {

	private static final int content_max_count=400;
	private static final Logger log = new Logger();
	
	@SuppressWarnings("unchecked")
	public static List removeSame(List arry) {

		Set set = new LinkedHashSet();
		for(Object tmp:arry){
            set.add(tmp);			
		}

		return Arrays.asList(set.toArray());   
	}
	
	
	
	public static String getPureText(String content,int cotentSize){
		if(content==null)
			return null;
		Parser parser = Parser.createParser(content, "GBK");
		NodeFilter textFilter = new NodeClassFilter(TextNode.class);
		StringBuffer retContent = new StringBuffer();
		NodeList p;
		try {
			p = parser.extractAllNodesThatMatch(textFilter);
			for (int i = 0; i < p.toNodeArray().length; i++) {
			    Node one = p.toNodeArray()[i];
			    retContent.append(one.toPlainTextString());
		    }
		} catch (ParserException e) {
			e.printStackTrace();
			log.error("parser content error:content="+content);
			return null;
		}
		if(retContent.length()>cotentSize){
			return retContent.substring(0, cotentSize)+"...";
		}
		    return retContent.toString();

	} 
	
	public static String getTxtFromHtml(String contentStr) throws Exception {
		   contentStr=contentStr.replaceAll("<[.[^<]]*>",""); 
		if (contentStr.length() > content_max_count) {
			contentStr = contentStr.substring(0, content_max_count) + "...";
		}
		return contentStr;
	}
	
	
	public static String[] reversalArray(String[]ary){
		if(ary==null||ary.length==0)
			return null;
		String[] newList=new String[ary.length];
		for(int i=ary.length-1;i>=0;i--)
		  newList[ary.length-i-1]=ary[i];
		return newList;
	}
}
